package cn.yinyu.queue.module.ecg.dal.mysql.room;

import java.util.*;

import cn.yinyu.queue.framework.common.pojo.PageResult;
import cn.yinyu.queue.framework.mybatis.core.mapper.BaseMapperX;
import cn.yinyu.queue.framework.mybatis.core.query.LambdaQueryWrapperX;
import cn.yinyu.queue.module.ecg.dal.dataobject.room.RoomDO;
import org.apache.ibatis.annotations.*;

import cn.yinyu.queue.module.ecg.controller.admin.room.vo.RoomPageReqVO;
import cn.yinyu.queue.module.ecg.dal.dataobject.room.RoomStatisticsDO;
import cn.yinyu.queue.module.ecg.enums.BedStatusEnum;

/**
 * 诊室和诊疗床 Mapper
 *
 * @author 垠域源码
 */
@Mapper
public interface RoomMapper extends BaseMapperX<RoomDO> {

    default PageResult<RoomDO> selectPage(RoomPageReqVO reqVO) {
        return selectPage(reqVO, new LambdaQueryWrapperX<RoomDO>()
                .eqIfPresent(RoomDO::getRoomId, reqVO.getRoomId())
                .eqIfPresent(RoomDO::getBedNo, reqVO.getBedNo())
                .betweenIfPresent(RoomDO::getCreateTime, reqVO.getCreateTime())
                .orderByDesc(RoomDO::getId));
    }

    @Select("select * from yy_queue.clinic_room where ip = #{ip}")
    List<RoomDO> queueByIp(@Param("ip") String ip);

    @Update("<script> " +
            "update yy_queue.clinic_room set status=#{newStatus}, doc_id=null, doc_name=null " +
            " where deleted = 0 " +
            "</script>")
    Integer resetRoom(@Param("newStatus") Integer newStatus);

    @Select({ "SELECT room_id, room_name, count(1) as bed_num FROM yy_queue.clinic_room group by room_id;" })
    List<RoomStatisticsDO> roomStatistic();

    @Select("<script> " +
            "SELECT * FROM yy_queue.clinic_room " +
            "where status in ( " +
                    " <foreach collection='curStatusList' separator=',' item='curStatus'>" +
                    "  #{curStatus.status} " +
                    " </foreach> )" +
            " and deleted = 0" +
            " order by room_id;" +
            "</script>")
    @Results({
            @Result(property = "checkTypes", column = "check_types", typeHandler=com.baomidou.mybatisplus.extension.handlers.JacksonTypeHandler.class)
    })
    List<RoomDO> simpleRoomList(@Param("curStatusList") List<BedStatusEnum> curStatusList);

    @Select("SELECT * FROM yy_queue.clinic_room where room_id=#{roomId} and bed_no=#{bedNo}")
    @Results({
        @Result(property = "checkTypes", column = "check_types", typeHandler=com.baomidou.mybatisplus.extension.handlers.JacksonTypeHandler.class)
    })
    RoomDO getRoom(@Param("roomId")Long roomId, @Param("bedNo")String bedNo);

    @Select("SELECT * FROM yy_queue.clinic_room where room_id=#{roomId} and bed_no=#{bedNo} and doc_id=#{docId}")
    RoomDO getRoomByRoomBedDoc(@Param("roomId")Long roomId, @Param("bedNo")String bedNo, @Param("docId")Long docId);

    @Select("SELECT * FROM yy_queue.clinic_room where doc_id=#{docId}")
    List<RoomDO> getRoomByDocId(@Param("docId")Long docId);

    @Update("<script> " +
            "update yy_queue.clinic_room set status=#{newStatus.status} " +
            " where room_id=#{roomId} and bed_no=#{bedNo} and isNull(doc_id) " +
            " and status in ( " +
            "  <foreach collection='curStatusList' separator=',' item='curStatus'>" +
            "   #{curStatus.status} " +
            "  </foreach> );" +
            "</script>")
    Integer setBedOpeningOpening(@Param("roomId")Long roomId, @Param("bedNo")String bedNo, @Param("newStatus") BedStatusEnum newStatus, @Param("curStatusList") List<BedStatusEnum> curStatusList);

    @Update("<script> " +
            "update yy_queue.clinic_room set status=#{newStatus.status}, doc_id=null, doc_name=null " +
            " where room_id=#{roomId} and bed_no=#{bedNo} and isNull(doc_id) " +
            " and status in ( " +
            "  <foreach collection='curStatusList' separator=',' item='curStatus'>" +
            "   #{curStatus.status} " +
            "  </foreach> );" +
            "</script>")
    Integer setBedOpeningClosed(@Param("roomId")Long roomId, @Param("bedNo")String bedNo, @Param("newStatus") BedStatusEnum newStatus, @Param("curStatusList") List<BedStatusEnum> curStatusList);

    @Update("<script> " +
            "update yy_queue.clinic_room set status=#{newStatus.status} " +
            "where room_id=#{roomId} and bed_no=#{bedNo} and doc_id=#{docId} " +
            "and status in ( " +
            " <foreach collection='curStatusList' separator=',' item='curStatus'>" +
            "  #{curStatus.status} " +
            " </foreach> );" +
            "</script>")
    Integer setBedDoctorStatus(@Param("roomId")Long roomId, @Param("bedNo")String bedNo, @Param("docId")Long docId, @Param("newStatus") BedStatusEnum newStatus, @Param("curStatusList") List<BedStatusEnum> curStatusList);

    @Update("<script> " +
            "update yy_queue.clinic_room set status=#{newStatus.status}, doc_id=#{docId}, doc_name=#{docName} " +
            "where room_id=#{roomId} and bed_no=#{bedNo} and isNull(doc_id)" +
            "and status in ( " +
            " <foreach collection='curStatusList' separator=',' item='curStatus'>" +
            "  #{curStatus.status} " +
            " </foreach> );" +
            "</script>")
    Integer setBedDoctorOn(@Param("roomId")Long roomId, @Param("bedNo")String bedNo, @Param("docId")Long docId, @Param("docName")String docName, @Param("newStatus") BedStatusEnum newStatus, @Param("curStatusList") List<BedStatusEnum> curStatusList);

    @Update("<script> " +
            "update yy_queue.clinic_room set status=#{newStatus.status}, doc_id=null, doc_name=null " +
            "where room_id=#{roomId} and bed_no=#{bedNo} and doc_id=#{docId} " +
            "and status in ( " +
            " <foreach collection='curStatusList' separator=',' item='curStatus'>" +
            "  #{curStatus.status} " +
            " </foreach> );" +
            "</script>")
    Integer setBedDoctorOff(@Param("roomId")Long roomId, @Param("bedNo")String bedNo, @Param("docId")Long docId, @Param("newStatus") BedStatusEnum newStatus, @Param("curStatusList") List<BedStatusEnum> curStatusList);
}
